/*********************************************************************************************************
Class Name: NMROpenAdsell
* Author: Accenture 
* Date: 29-Mar-2012  
* Requirement/Project Name: NMR 
* Description: Constructor for NMRShellCustomerLoad, NMRAdSellLaunchLead.
*              To get Customer Id from URL
* Version: 0.1 Updated to pick application Id from custom settings
*********************************************************************************************************/

public class NMROpenAdsell {
   
    public string custId{get; set;}
    public string newPageId{get; set;}
    public string applId{get; set;}
    public Lead lead = new Lead();
    private AppEndPoints__c appEndPoint = AppEndPoints__c.getInstance();
    
    /**
    * The constructor retrieves the CustId from the URL parameter
    * Also retrieves the AdSell Env from the Custom Settings.
    */   
    public NMROpenAdsell(ApexPages.StandardController controller) {
            
        custId = ApexPages.currentPage().getParameters().get('CustId');
        newPageId = ApexPages.currentPage().getParameters().get('PageId');
        applId = appEndPoint.AdSell_Env__c;
        this.lead =  (Lead)controller.getRecord();           
    }
    
    public void createOpportunity(){
        lead.NMR_Convert_Lead__c = true;
        
        if((String.valueOf(lead.ownerId)).startsWith('00G'))
            lead.ownerId = UserInfo.getUserId();
            
        try{
            update lead;
        } catch(DMLException e) {
            system.debug('Unable to update Lead. Error message:' + e.getMessage());    
        }
        newPageId = String.valueOf(NMRCreateOpportunity.convertLead(lead));
    }    
    
    //Test Method
    @isTest(SeeAllData=true)    
    static void NMROpenAdsellTest() {
        
        test.startTest();
             
        PageReference pageRef = Page.NMRShellCustomerLoad;
        pageRef.getParameters().put('CustId', '1234567');
        Test.setCurrentPageReference(pageRef);
        
        profile profid = [Select Id from profile q where profile.Name ='IR Leadership'];
        User singleUser = new User(alias = 'Test101', email='nmradminTest101@accenture.com',emailencodingkey='UTF-8', lastname='Test102', languagelocalekey='en_US',localesidkey='en_US', profileid = profid.id,timezonesidkey='America/Los_Angeles', username='nmrtest101@testorg.com',SFA_Office__c='NYC',IsActive = True,css_office_name__c = 'NYC',ATTUID__c = 'SC352J');              
        insert singleUser;
        
        Lead lead = new Lead(lastName = 'Test Lead',recordtypeid=RecordTypeHelper.GetRecordTypeID('Lead', 'NMR Lead'),phone='9992995645',Company='testcomp',OwnerId=singleUser.Id);
        insert lead;
        
        
        ApexPages.StandardController sc = new ApexPages.standardController(lead);        
        NMROpenAdsell openAdSell = new NMROpenAdsell(sc);
        openAdSell.lead = lead;
        openAdSell.createOpportunity();
        
        test.stopTest();
    }
}